package com.hfplm.lhh.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;

import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCComponentItemType;
import com.teamcenter.rac.kernel.TCComponentProcess;
import com.teamcenter.rac.kernel.TCComponentTask;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;

public class AddtoProcess extends AbstractHandler{
	
	private TCSession session;
	private AbstractAIFUIApplication app;
	InterfaceAIFComponent target;
	@Override
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		app = AIFUtility.getCurrentApplication();
		session = (TCSession)app.getSession();
		Runnable runnable = new Runnable() {

			@Override
			public void run() {
				target = app.getTargetComponent();
				System.out.println(target.getType());
				if(target instanceof TCComponentTask) {
					TCComponentTask task = (TCComponentTask) target;
					try {
						System.out.println(task.getRelatedComponent("task_template"));
					} catch (TCException e) {
						e.printStackTrace();
					}
					
				}
				System.out.println(target.getClass());
				if(!(target instanceof TCComponentItemRevision)) {
					return;
				}
				TCComponentItemRevision revision = (TCComponentItemRevision) target;
				
				try {
					TCComponentItemType itemTypeComponent = (TCComponentItemType)session.getTypeComponent("Item");
					TCComponentItemRevision revision2 = itemTypeComponent.find("ECN006502").getLatestItemRevision();
					
					TCComponentProcess process = revision.getCurrentJob();
					TCComponent c = process.getRelatedComponent("task_template");
					System.out.println(c);
					String s = process.getName();
					System.out.println(s);
					System.out.println(process.getProperty("current_name"));
					System.out.println(process.getProperty("object_name"));
					// process.getRootTask().addAttachments(TCAttachmentScope.GLOBAL, new TCComponent[]{revision2}, new int[]{1});
					process.getRootTask().add("root_target_attachments", revision2);
					System.out.println(process);
				} catch (TCException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
			
		};
		new Thread(runnable).start();
		return null;
	}

}
